package com.example.app.components.MyScaffold

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import com.example.app.ui.theme.my_border_color

//因为 Scaffold默认占满全屏，所以要自定义写一个来处理不是全屏的情况
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyCustomScaffold(
        modifier: Modifier = Modifier,
        topBar: (@Composable BoxScope.() -> Unit)? = null,
        bottomBar: (@Composable BoxScope.() -> Unit)? = null,
        content: (@Composable BoxScope.() -> Unit)? = null,
        topBarDivider: (@Composable () -> Unit)? = null,
        bottomBarDivider: (@Composable () -> Unit)? = null,
) {
    var density = LocalDensity.current

    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.Start,
        verticalArrangement = Arrangement.Top
    ) {
        //topbar
        if (topBar != null) {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(TopAppBarDefaults.TopAppBarExpandedHeight)
            ) {
                topBar()
            }
            if (topBarDivider != null) {
                topBarDivider()
            }
        }
        //content
        if (content != null) {
            Box {
                content()
            }
        }
        //bottomBar
        if (bottomBar != null) {
            if (bottomBarDivider != null) {
                bottomBarDivider()
            }

            Box(
                modifier = Modifier
                    .drawBehind {
                        drawLine(
                            color = my_border_color,
                            start = Offset(
                                0f,
                                0f
                            ),
                            end = Offset(
                                size.width,
                                0f
                            ),
                            strokeWidth = Stroke(
                                width = 1.dp.toPx(),
                                cap = StrokeCap.Round
                            ).width
                        )
                    }
                    .fillMaxWidth()
                    .height(TopAppBarDefaults.TopAppBarExpandedHeight)
                //.height(BottomAppBarDefaults.windowInsets.getBottom(density).dp)
            ) {
                bottomBar()
            }
        }
    }
}

